<?php
require_once 'init.php';
if(!$sessionID){
	header('location:login.php');
}
$id = isset($_POST['id'])?$_POST['id']:0;
$src = isset($_POST['src'])?$_POST['src']:'';
$action = isset($_POST['action'])?$_POST['action']:'';
$albumID = isset($_POST['albumID'])?$_POST['albumID']:0;
if($id==0||!in_array($action,array('home','wedding','studio','cooperation','guest'))){
	header('location:index.php');
}
if($action!='home'&&$albumID==0){
	header('location:img.php?action='.$action);
}
if($action=='home'){
	$page = 'page1';
}elseif($action=='wedding'){
	$page = 'page3';
}elseif($action=='studio'){
	$page = 'page4';
}elseif($action=='cooperation'){
	$page = 'page6';
}elseif($action=='guest'){
	$page = 'page7';
}
$dom = new DOMDocument('1.0','UTF-8');
$dom->load($contentDB);
$pages = $dom->getElementsByTagName($page);
$photo = $pages->item(0);
$items = array();
if($action=='home'){
	$items = $photo->getElementsByTagName('img');
}else{
	if($albumID==0){
		header('location:index.php');
	}
	$albums = $photo->getElementsByTagName('photo');
	foreach($albums as $album){
		if($album->getAttribute('id')==$albumID){
			$items = $album->getElementsByTagName('img');
		}
	}
}
if(empty($items)){
	header('location:img.php?action='.$action);
}
foreach($items as $item){
	$imgId = $item->getAttribute('id');
	if((int)$imgId == (int)$id){
		$item->nodeValue = $src;
	}
}
$dom->save($contentDB);
XmlTool::ADDUTF8BOM($contentDB);
header('location:img.php?action='.$action);
?>
